﻿using System;

class Unit
{
    protected int amount;      
    protected string owner;   
    protected int hp;          
    protected int att;
    protected int def;
    protected string name;
    protected int range;
    protected int movement;
    protected int currentX;
    protected int currentY;
    protected int curHP;

    public void takeDMG(int incomingDMG)//Unit Takes dmg (input is <int> Dmg dealt)
    {                            //Zadava nova stoynost na amount po zadadena formula i proverki.
        curHP -= (incomingDMG - (incomingDMG * def) / 100);//Takes the def as % from the incoming dmg and lowers the dmg.
        if (curHP <= 0)
        {
            amount = 0;
        }
        else
        {
            amount = curHP / hp;

            if (curHP % hp != 0)//if a unit is hurt, it still deals full dmg
            {                   
                amount++;
            }
        }
    }

    public int doDMG()//returns dmg dealt
    {
        int dmgDealt = att * amount;
        return dmgDealt;
    }

    public bool isDead()//Checks if the unit is dead or not existing
    {
        if (amount == 0)
            return true;
        else
            return false;
    }

    public bool Move(int x , int y , string[,] board)
    {
        if (Math.Abs((currentX - (x * 2 - 1)) + (currentY - (y * 2 - 1))) <= movement*2)
        {
            if(board[y*2-1,x*2-1]=="     ")
            {
                board[currentY, currentX] = "     ";
                currentX = x*2-1;
                currentY = y*2-1;
            }
            else
                Console.WriteLine("The position is occupied");
            return true;
        }
        else
        {
            Console.WriteLine("The unit can't move this far");
            return false;
        }

    }

    public bool Attack(int x, int y, string[,] board)
    {
        if (Math.Abs(Math.Abs(currentX - (x * 2 - 1)) + Math.Abs(currentY - (y * 2 - 1))) <= range * 2)
        {
            if (board[y * 2 - 1, x * 2 - 1] == "     ")
            {
                Console.WriteLine("There is noone to attack");
                return false;
            }
            else
            {
                if (board[y * 2 - 1, x * 2 - 1].Substring(0,2)==owner)
                {
                    Console.WriteLine("Friendly Fire is not allowed");
                    return false;
                }
                else
                    return true;
            }    
        }
        else
        {
            Console.WriteLine("The unit can't attack this far");
            return false;
        }

    }

    public int X
    {
        get
        {
            return currentX;
        }
    }

    public int Y
    {
        get
        {
            return currentY;
        }
    }


    public string Name
    {
        get
        {
            return name;
        }
    }
    public int Range
    {
        get
        {
            return range;
        }
    }
    public int Movement
    {
        get
        {
            return movement;
        }
    }
    public int Amount
    {
        get
        {
            return amount;
        }
    }

  
}
